package com.example.gridview_demo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class Gridview_Adapter extends BaseAdapter {
    private List<Gridview_mode> list;
    private Context context;

    public Gridview_Adapter(List<Gridview_mode> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v=null;
        ViewHolder holder=null;
        if (view==null)
        {
            v= LayoutInflater.from(context).inflate(R.layout.gridview_mode,viewGroup,false);
            holder=new ViewHolder();
            holder.img=v.findViewById(R.id.img);
            holder.text=v.findViewById(R.id.text);
            v.setTag(holder);
        }else
        {
            v=view;
            holder = (ViewHolder) v.getTag();
        }
        holder.img.setImageResource(list.get(i).getImg());
        holder.text.setText(list.get(i).getText());

        return v;
    }
    class ViewHolder{
        ImageView img;
        TextView text;
    }
}
